<?php

namespace pmvc\mvc\controller;

use pmvc\mvc\Controller;
use pmvc\mvc\Dispatcher;
use pmvc\mvc\Model;

class BaseFormController
	implements Controller {
	
	public $formView		= FALSE;
	public $errorView		= FALSE;
	public $successView		= FALSE;
	
	private $errors = Array();
	
	protected function addGlobalError($message) {
		if (!array_key_exists("globalErrors", $this->errors)) {
			$this->errors["globalErrors"] = Array();
		}
		$this->errors["globalErrors"][] = $message;
	}
	
	protected function addFieldError($field, $message) {
		if (!array_key_exists("fieldErrors", $this->errors)) {
			$this->errors["fieldErrors"] = Array();
		}
		if (!array_key_exists($field, $this->errors["fieldErrors"])) {
			$this->errors["fieldErrors"][$field] = Array();
		}
		$this->errors["fieldErrors"][$field][] = $message;
	}
	
	protected function clearFieldErrors($field=FALSE) {
		if (!array_key_exists("fieldErrors", $this->errors)) {
			return;
		} else if ($field===FALSE) {
			$this->errors["fieldErrors"] = Array();
		} else {
			$this->errors["fieldErrors"][$field] = Array();
		}
	}
	
	protected function clearGlobalErrors() {
		if (!array_key_exists("globalErrors", $this->errors)) {
			return;
		} else {
			$this->errors["globalErrors"] = Array();
		}
	}
	
	protected function clearAllErrors() {
		$this->errors = Array();
	}
	
	protected function hasErrors() {
		if (array_key_exists("globalErrors", $this->errors)
			&& count($this->errors["globalErrors"])>0) {
			return true;
		} else if (array_key_exists("fieldErrors", $this->errors)) {
			foreach ($this->errors["fieldErrors"] as $fieldError) {
				if (count($fieldError)>0) {
					return true;
				}
			}
		}
		return false;
	}
	
	public function isFormSubmission(Dispatcher $dispatcher) {
		return ($_SERVER["REQUEST_METHOD"]=="POST");
	}
	
	public function populateModel(Model $model, Dispatcher $dispatcher) {
		if ($this->hasErrors()) {
			$model["formErrors"] = $this->errors;
		}
		return;
	}
	
	public function showForm(Model $model, Dispatcher $dispatcher) {
		\pmvc\log\Log::debug("BaseFormController::showForm()");
		$this->populateModel($model, $dispatcher);
		return $this->formView;
	}
	
	public function validateForm(Model $model, Dispatcher $dispatcher) {
		return TRUE;
	}
	
	public function showValidationError(Model $model, Dispatcher $dispatcher) {
		\pmvc\log\Log::debug("BaseFormController::showValidationError()");
		$this->populateModel($model, $dispatcher);
		return $this->errorView;
	}
	
	public function processForm(Model $model, Dispatcher $dispatcher) {
		return TRUE;
	}
	
	public function showProcessError(Model $model, Dispatcher $dispatcher) {
		\pmvc\log\Log::debug("BaseFormController::showProcessError()");
		$this->populateModel($model, $dispatcher);
		return $this->errorView;
	}
	
	public function showSuccess(Model $model, Dispatcher $dispatcher) {
		\pmvc\log\Log::debug("BaseFormController::showSuccess()");
		$this->populateModel($model, $dispatcher);
		return $this->successView;
	}
	
	/**
	 * {@inheritDoc}
	 */
    public final function handleRequest(Model $model, Dispatcher $dispatcher) {
    	
    	// send to formView
    	if (!$this->isFormSubmission($dispatcher)) {
    		return $this->showForm($model, $dispatcher);
    		
    	// proccess form submission
    	} else if (!$this->validateForm($model, $dispatcher)) {
    		return $this->showValidationError($model, $dispatcher);
    			
    	// proccess form submission
    	} else if (!$this->processForm($model, $dispatcher)) {
    		return $this->showProcessError($model, $dispatcher);
    		
    	// submission was successful
    	} else {
    		return $this->showSuccess($model, $dispatcher);
    	}
    }

}

?>